package edu.ynu.se.xiecheng.achitectureclass.common.service;

import edu.ynu.se.xiecheng.achitectureclass.common.dao.CommonRecodeDAO;
import edu.ynu.se.xiecheng.achitectureclass.common.dao.DeviceDAO;
import edu.ynu.se.xiecheng.achitectureclass.common.dao.GreenhouseDAO;
import edu.ynu.se.xiecheng.achitectureclass.common.dto.CommonDeviceValueDTO;
import edu.ynu.se.xiecheng.achitectureclass.common.entity.CommonRecode;
import edu.ynu.se.xiecheng.achitectureclass.common.entity.Device;
import edu.ynu.se.xiecheng.achitectureclass.common.entity.Greenhouse;
import edu.ynu.se.xiecheng.achitectureclass.common.utils.CommonDeviceTokenUtil;
import edu.ynu.se.xiecheng.achitectureclass.common.utils.CommonDeviceValueUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

@Service
public class CommonRecodeService {

    @Autowired
    private CommonRecodeDAO commonRecodeDAO;

    @Autowired
    private DeviceDAO deviceDAO;

    @Autowired
    private GreenhouseDAO greenhouseDAO;

    public HashMap<String, CommonRecode> ObtainDataFromServer(String commonDeviceToken) throws IOException {
        CommonDeviceTokenUtil commonDeviceTokenUtil = new CommonDeviceTokenUtil();
        CommonDeviceValueUtil commonDeviceValueUtil = new CommonDeviceValueUtil();
        Long greenhouseId = 1L;
        List<Device> deviceList = deviceDAO.findByGreenhouseId(greenhouseId);
        List<String> commonIds = new LinkedList<>();
        List<String> cameraIds = new LinkedList<>();
        for (Device device : deviceList) {
            if (device.getDeviceType() != 9){
                commonIds.add(device.getSeriesId());
            }
            else {
                cameraIds.add(device.getSeriesId());
            }
        }
        List<CommonDeviceValueDTO> commonDeviceValueDTOS = commonDeviceValueUtil.getDeviceValues(commonIds, "Bearer "+commonDeviceToken);

        HashMap<String, CommonRecode> commonRecodeHashMap = new HashMap<>();
       for (CommonDeviceValueDTO commonDeviceValueDTO : commonDeviceValueDTOS) {
           CommonRecode commonRecode = new CommonRecode();
           commonRecode.setIsDeleted(0);
           commonRecode.setDeviceSeriesId(commonDeviceValueDTO.getId());
           commonRecode.setName(commonDeviceValueDTO.getName());
           commonRecode.setValue(commonDeviceValueDTO.getValue());
           commonRecode.setTimeStamp(String.valueOf(commonDeviceValueDTO.getTimestamp()));
           commonRecodeHashMap.put(commonDeviceValueDTO.getId(), commonRecodeDAO.save(commonRecode));
       }
//        for(String key: commonRecodeHashMap.keySet()){
//            System.out.println("Key: " + key + ", Value: " + commonRecodeHashMap.get(key).toString());
//        }
        return commonRecodeHashMap;
    }

    public  String getCommonToken() throws IOException {
        return CommonDeviceTokenUtil.getToken().getAccess_token();
    }
}
